package com.example.medispider.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import us.codecraft.webmagic.Site;

@Configuration
public class SiteConfig {

    @Value("${spring.medi-spider.site.retryTimes}")
    private int retryTimes;
    @Value("${spring.medi-spider.site.sleepTime}")
    private int sleepTime;
    @Value("${spring.medi-spider.site.timeout}")
    private int timeout;
    @Value("${spring.medi-spider.site.cookie.SESSION}")
    private String SESSION;
    @Value("${spring.medi-spider.site.cookie.XSRF-TOKEN}")
    private String XSRF_TOKEN;
    @Value("${spring.medi-spider.site.header.Content-Type}")
    private String Content_Type;
    @Value("${spring.medi-spider.site.User-Agent}")
    private String User_Agent;
    @Value("${spring.medi-spider.site.header.Accept}")
    private String Accept;
    @Value("${spring.medi-spider.site.header.Accept-Encoding}")
    private String Accept_Encoding;
    @Value("${spring.medi-spider.site.header.Accept-Language}")
    private String Accept_Language;
    @Value("${spring.medi-spider.site.header.Connection}")
    private String Connection;
    @Value("${spring.medi-spider.site.header.Host}")
    private String Host;
    @Value("${spring.medi-spider.site.header.Origin}")
    private String Origin;
    @Value("${spring.medi-spider.site.header.Referer}")
    private String Referer;
    @Value("${spring.medi-spider.site.header.Sec-Fetch-Dest}")
    private String Sec_Fetch_Dest;
    @Value("${spring.medi-spider.site.header.Sec-Fetch-Mode}")
    private String Sec_Fetch_Mode;
    @Value("${spring.medi-spider.site.header.Sec-Fetch-Site}")
    private String Sec_Fetch_Site;
    @Value("${spring.medi-spider.site.header.sec-ch-ua}")
    private String sec_ch_ua;
    @Value("${spring.medi-spider.site.header.sec-ch-ua-mobile}")
    private String sec_ch_ua_mobile;
    @Value("${spring.medi-spider.site.header.sec-ch-ua-platform}")
    private String sec_ch_ua_platform;

    /**
     * 注意：Content-Length 不需要手动设置，HttpClient 会自动计算
     * Content-Type 在设置请求体时会自动设置，但如果需要可以显式指定
     * .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
     *
     * @return Site
     */
    @Bean(name = "site")
    public Site site() {
        // 创建并配置 Site 对象
        Site site = Site.me()
                .setRetryTimes(retryTimes)
                .setSleepTime(sleepTime)
                .setTimeOut(timeout)
                // 设置请求头
                .setUserAgent(User_Agent)
                .addHeader("Content-Type", Content_Type)
                .addHeader("Accept", Accept)
                .addHeader("Accept-Encoding", Accept_Encoding)
                .addHeader("Accept-Language", Accept_Language)
                .addHeader("Connection", Connection)
                .addHeader("Host", Host)
                .addHeader("Origin", Origin)
                .addHeader("Referer", Referer)
                .addHeader("Sec-Fetch-Dest", Sec_Fetch_Dest)
                .addHeader("Sec-Fetch-Mode", Sec_Fetch_Mode)
                .addHeader("Sec-Fetch-Site", Sec_Fetch_Site)
                .addHeader("X-XSRF-TOKEN", XSRF_TOKEN)
                .addHeader("sec-ch-ua", sec_ch_ua)
                .addHeader("sec-ch-ua-mobile", sec_ch_ua_mobile)
                .addHeader("sec-ch-ua-platform", sec_ch_ua_platform)
                // 设置 Cookie
                .addCookie("XSRF-TOKEN", XSRF_TOKEN)
                .addCookie("SESSION", SESSION);
        return site;
    }
}
